<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 */

namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\admin\logic\SearchWordLogic;
use app\common\model\Column;
use app\common\model\GoodsAttr;
use app\common\model\GoodsAttribute;
use app\common\model\GoodsType;
use app\common\model\Spec;
use app\common\model\SpecGoodsPrice;
use app\common\model\SpecItem;
use app\common\model\GoodsCategory;
use think\AjaxPage;
use think\Loader;
use think\Page;
use think\Db;

class Home extends Base
{

    /**
     *  栏目分类列表
     */
    public function columnList(){

        $navigationList = M('column')->where('pid=0')->order('id asc')->select();

        if($navigationList){

            foreach ($navigationList as $k=>$v){

                $navigationList[$k]['typeName'] = C('column_list')[$v['type']];

                $child = M('column')->where('pid='.$v['id'])->order('id asc')->select();

                if((bool)$child){

                    foreach ($child as $kk=>$vv){

                        $child[$kk]['typeName'] = C('column_list')[$vv['type']];
                        $navigationList[$k]['child'][$kk] = $child[$kk];

                        $children = M('column')->where('pid='.$vv['id'])->order('id asc')->select();
                        if((bool)$children){
                            foreach ($children as $kkk=>$vvv){

                                $children[$kkk]['typeName'] = C('column_list')[$vvv['type']];
                            }
                            $navigationList[$k]['child'][$kk]['child'] = $children;
                        }
                    }
                }
            }
        }
        $this->assign('navigationList', $navigationList);
        return $this->fetch();
    }

    /**
     * 添加修改栏目
     */
    public function addEditColumn(){

        if (IS_GET) {

            $id = intval(I('id',0));
            if($id){

                $navigation = M('column')->where('id='.$id)->find();
                $this->assign('navigation', $navigation);
            }

            $type = config('column_list');
            $columnList = Column::getInstance()->getAllColumnList();

            $this->assign('type', $type);
            $this->assign('columnList', $columnList);
            return $this->fetch();
            exit;
        }

        $type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        //ajax提交验证
        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $new_id = 0;
            $data = input('post.');

            if($data['id']){

                $id = $data['id'];
                unset($data['id']);
            }

            if($data['pic']){
                $tagUrl = base64_image_content($data['pic'],'/public/upload/column');
                if((bool)$tagUrl){
                    $data['pic'] = $tagUrl;
                }
            }

            DB::startTrans();

            if($type == 1){

                $data['add_time'] = time();
                $new_id = $res = M('column')->add($data);
            }else{
                $res = M('column')->where('id='.$id)->save($data);
                $new_id = $id;
            }

            if( ! $res){
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '操作失败',
                    'url'   => U('Admin/Home/columnList')
                );
                $this->ajaxReturn($return_arr);
                Db::rollback();
            }

            $arr = [];
            if((bool)$data['pid']){
                $arr['path'] = '|0|'.$data['pid'].'|'.$new_id.'|';
            }else{
                $arr['path'] = '|'.$data['pid'].'|'.$new_id.'|';
            }

            M('column')->where('id='.$new_id)->save($arr);

            Db::commit();

            $return_arr = array(
                'status' => 1,
                'msg'   => '操作成功',
                'url'=>U('Admin/Home/columnList')
            );
            $this->ajaxReturn($return_arr);

        }

    }

    /**
     * 删除分类
     */
    public function delColumn(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);
        // 判断子分类
        $count = Db::name("column")->where("pid = {$ids}")->count("id");
        $count > 0 && $this->ajaxReturn(['status' => -1, 'msg' => '该分类下还有分类不得删除!']);
        // 删除分类
        DB::name('column')->where('id', $ids)->delete();
        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U('Admin/Home/columnList')]);
    }

    /**
     * 参数管理--最新公告
     * */
    public function saveIndexNotice(){

        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $data = input('post.');

            $res = M('admin_config')->where('id=1')->save($data);

            if( ! $res){
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '保存失败',
                    'url'   => U('Admin/Home/saveIndexNotice')
                );
                $this->ajaxReturn($return_arr);
            }

            $return_arr = array(
                'status' => 1,
                'msg'   => '保存成功',
                'url'=>U('Admin/Home/saveIndexNotice')
            );
            $this->ajaxReturn($return_arr);

        }

        $info = M('admin_config')->where('id=1')->field('site_notice')->find();

        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 参数管理--联系文
     * */
    public function saveAbout(){

        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $data = input('post.');

            $site_server_tel = [];
            if(count($data['link_name']) >0 ){

                if(count($data['link_name']) != count($data['link_url'])){

                    $this->ajaxReturn(['status' => 1,'msg' => '操作失败，客服qq参数错误','url'=>'']);
                }

                for ($i=0;$i<count($data['link_name']);$i++){

                    if( ! $data['link_name'][$i]){
                        unset($data['link_url'][$i]);
                    }else{

                        $site_server_tel[$data['link_name'][$i]] = (bool)$data['link_url'][$i] ?$data['link_url'][$i]: '';
                    }
                }
            }

            $data['site_server_tel'] = json_encode(array_filter($site_server_tel));
            unset($data['link_name']);
            unset($data['link_url']);

            $res = M('admin_config')->where('id=1')->save($data);

            if( ! $res){
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '保存失败',
                    'url'   => U('Admin/Home/saveIndexNotice')
                );
                $this->ajaxReturn($return_arr);
            }

            $return_arr = array(
                'status' => 1,
                'msg'   => '保存成功',
                'url'=>U('Admin/Home/saveIndexNotice')
            );
            $this->ajaxReturn($return_arr);

        }

        $info = M('admin_config')->where('id=1')->find();

        if($info['site_server_tel']){
            $link = (array)json_decode($info['site_server_tel']);
            if($link){
                $res = [];
                foreach($link as $k=>$v){
                    $res[] = [
                        'name' => $k,
                        'url' => $v
                    ];
                }
                $info['site_server_tel'] = $res;
            }
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 参数管理--基本设置
     * */
    public function saveParams(){

        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $data = input('post.');

            if($data['site_ico']){

                $tagUrl = base64_image_content($data['site_ico'],'/public/upload/ico');
                if((bool)$tagUrl){
                    $data['site_ico'] = $tagUrl;
                }
            }else{
                unset($data['site_ico']);
            }

            $res = M('admin_config')->where('id=1')->save($data);

            if( ! $res){
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '保存失败',
                    'url'   => U('Admin/Home/saveIndexNotice')
                );
                $this->ajaxReturn($return_arr);
            }

            $return_arr = array(
                'status' => 1,
                'msg'   => '保存成功',
                'url'=>U('Admin/Home/saveIndexNotice')
            );
            $this->ajaxReturn($return_arr);

        }

        $info = M('admin_config')->where('id=1')->find();

        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     *  位置列表
     */
    public function advertList(){

        $advert = M('advert')->order('id desc')->select();
        $position = C('position_list');
        if($advert){
            foreach ($advert as $k=>$v){

                $advert[$k]['typeName'] = isset($position[$v['position']]) ? $position[$v['position']] : '无';
            }
        }

        $this->assign('advert', $advert);
        return $this->fetch();
    }

    /**
     * 添加修改广告
     */
    public function addEditAdvert(){

        if (IS_GET) {

            $id = intval(I('id',0));
            if($id){

                $navigation = M('advert')->where('id='.$id)->find();
                $this->assign('navigation', $navigation);
            }

            $type = config('column_list');
            $columnList = Column::getInstance()->getAllColumnList();

            $this->assign('id', $id);
            $this->assign('type', $type);
            $this->assign('columnList', $columnList);
            return $this->fetch();
            exit;
        }

        $type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        //ajax提交验证
        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $new_id = 0;
            $data = input('post.');

            $return_arr = array(
                'status' => 0,
                'msg'   => '暂未开放',
                'url'   => U('Admin/Home/advertList')
            );
            $this->ajaxReturn($return_arr);
            p($data);
            if($data['id']){

                $id = $data['id'];
                unset($data['id']);
            }

            if($type == 1){

                $data['create_time'] = time();
               $res = M('advert')->add($data);
            }else{
                $res = M('advert')->where('id='.$id)->save($data);

            }

            if($res){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'url'=>U('Admin/Home/advertList')
                );
                $this->ajaxReturn($return_arr);

            }else{
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '操作失败',
                    'url'   => U('Admin/Home/advertList')
                );
                $this->ajaxReturn($return_arr);

            }
        }
    }

    /**
     * 删除广告
     */
    public function delAdvert(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);

        // 删除分类
        DB::name('advert')->where('id', $ids)->delete();
        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U('Admin/Home/advertList')]);
    }

    /**
     *  文章列表
     */
    public function newsList(){

        $news = M('article_news')
            ->alias('a')
            ->join('column c','c.id=a.pid','left')
            ->field('a.*,c.name as typeName')
            ->where('c.pid != 7')
            ->order('id desc')
            ->select();

        $this->assign('news', $news);
        return $this->fetch();
    }

    /**
     * 添加修改文章
     */
    public function addEditNews(){

        if (IS_GET) {

            $id = intval(I('id',0));
            if($id){

                $navigation = M('article_news')->where('id='.$id)->find();
                $this->assign('navigation', $navigation);
            }

            $type = config('column_list');
            $columnList = Column::getInstance()->getAllColumnList(['nid'=>7]);

            $this->assign('id', $id);
            $this->assign('type', $type);
            $this->assign('columnList', $columnList);
            return $this->fetch();
            exit;
        }

        $type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        //ajax提交验证
        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $new_id = 0;
            $data = input('post.');

            if($data['id']){

                $id = $data['id'];
                unset($data['id']);
            }

            if($data['images']){
                $tagUrl = base64_image_content($data['images'],'/public/upload/news');
                if((bool)$tagUrl){
                    $data['images'] = $tagUrl;
                }
            }

            if($type == 1){

                $data['add_time'] = time();
                $res = M('article_news')->add($data);
            }else{

                $res = M('article_news')->where('id='.$id)->save($data);
            }

            if($res){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'url'=>U('Admin/Home/newsList')
                );
                $this->ajaxReturn($return_arr);

            }else{
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '操作失败',
                    'url'   => U('Admin/Home/newsList')
                );
                $this->ajaxReturn($return_arr);

            }
        }
    }

    /**
     * 删除文章
     */
    public function delNews(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);

        // 删除分类
        DB::name('article_news')->where('id', $ids)->delete();
        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U('Admin/Home/newsList')]);
    }

    /**
     *  开运金榜列表
     */
    public function newsPicList(){

        $news = M('article_news')
            ->alias('a')
            ->join('column c','c.id=a.pid','left')
            ->field('a.*,c.name as typeName')
            ->where('c.pid=7')
            ->order('id desc')
            ->select();

        $this->assign('news', $news);
        return $this->fetch();
    }

    /**
     * 添加开运金榜
     */
    public function addEditNewsPic(){

        if (IS_GET) {

            $id = intval(I('id',0));
            if($id){

                $navigation = M('article_news')->where('id='.$id)->find();
                $this->assign('navigation', $navigation);
            }

            $type = config('column_list');
            $columnList = Column::getInstance()->getAllColumnList(['tid'=>7]);

            $this->assign('id', $id);
            $this->assign('type', $type);
            $this->assign('columnList', $columnList);
            return $this->fetch();
            exit;
        }

        $type = I('id') > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        //ajax提交验证
        if (I('is_ajax') == 1 && IS_POST) {
            // 数据验证
            $id = 0;
            $new_id = 0;
            $data = input('post.');

            if($data['id']){

                $id = $data['id'];
                unset($data['id']);
            }

            if($data['images']){
                $tagUrl = base64_image_content($data['images'],'/public/upload/bang');
                if((bool)$tagUrl){
                    $data['images'] = $tagUrl;
                }
            }

            if($type == 1){

                $data['add_time'] = time();
                $res = M('article_news')->add($data);
            }else{

                $res = M('article_news')->where('id='.$id)->save($data);
            }

            if($res){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'url'=>U('Admin/Home/newsPicList')
                );
                $this->ajaxReturn($return_arr);

            }else{
                $return_arr = array(
                    'status' => 0,
                    'msg'   => '操作失败',
                    'url'   => U('Admin/Home/newsPicList')
                );
                $this->ajaxReturn($return_arr);

            }
        }
    }

    /**
     * 删除开运金榜
     */
    public function delNewsPic(){
        $ids = I('post.ids', '');
        empty($ids) && $this->ajaxReturn(['status' => -1, 'msg' => "非法操作！", 'data' => '']);

        // 删除分类
        DB::name('article_news')->where('id', $ids)->delete();
        $this->ajaxReturn(['status' => 1, 'msg' => '操作成功', 'url' => U('Admin/Home/newsList')]);
    }

}